package com.ergwang.dao;

import com.ergwang.Model.admin.vo.Msg_NoRepliedVO;
import com.ergwang.Model.admin.vo.Msg_RepliedVO;
import com.ergwang.Model.admin.vo.Msg_goodsVO;
import com.ergwang.Model.admin.vo.Msg_userVO;
import com.ergwang.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author：ergwang
 * @Date: 2020/6/1
 * @Time: 14:44
 */
public class MsgDaoImpl implements MsgDao {
    /**
     * 获取前端要求的数据的主体部分，封装为VO对象，返回给service
     * @param state 状态，0已回复 1未回复
     * @return 前端要求的VO对象
     */
    public List<Msg_NoRepliedVO> noReplyMsg(int state) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Msg_NoRepliedVO> query = null;
        try {
            query = runner.query("select id,userId,goodsId,content,state,createtime from massages where state = ?",
                    new BeanListHandler<Msg_NoRepliedVO>(Msg_NoRepliedVO.class),
                    state);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    /**
     * 获取VO的goods部分
     * @param goodsId
     * @return
     */
    public Msg_goodsVO msgGoods(Integer goodsId) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Msg_goodsVO query = null;
        try {
            query = runner.query("select name from goods where id = ?",
                    new BeanHandler<Msg_goodsVO>(Msg_goodsVO.class),
                    goodsId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    /**
     * 获取VO的user部分
     * @param userId
     * @return
     */
    public Msg_userVO msgUser(Integer userId) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Msg_userVO query = null;
        try {
            query = runner.query("select nickname as name from user where id = ?",
                    new BeanHandler<Msg_userVO>(Msg_userVO.class),
                    userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    public List<Msg_RepliedVO> ReplyMsg(int state) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Msg_RepliedVO> query = null;
        try {
            query = runner.query("select id,userId,goodsId,content,replyContent,state,createtime from massages where state = ?",
                    new BeanListHandler<Msg_RepliedVO>(Msg_RepliedVO.class),
                    state);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }
}
